package com.aimao.blog.util;

import com.aimao.blog.dao.ArticleMapper;
import com.aimao.blog.dao.SortMapper;
import com.aimao.blog.dao.SubjectMapper;
import com.aimao.blog.pojo.Subject;
import com.aimao.blog.vo.ArticleAndCommentCountVo;
import com.aimao.blog.vo.SortAndArticleCountVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author PuaChen
 * @Create 2018-08-01 17:12
 */
@Component
public class SitemapFactory {

    @Autowired
    private SortMapper sortMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private SubjectMapper subjectMapper;

    /**
     * 创建标签
     *
     * @param url
     * @return
     */
    private Element createUrlForXML(String url) {
        if (!url.startsWith("http://")) {
            url = "http://" + url;
        }
        Element root = DocumentHelper.createElement("url");
        root.addElement("loc").addText(url);
        root.addElement("priority").addText("0.5");
        root.addElement("lastmod").addText(DateTime.now().toString("yyyy-MM-dd"));
        root.addElement("changefreq").addText("weekly");
        return root;
    }


    public String getResultForXML() {
        Element root = DocumentHelper.createElement("urlset");
        root.addAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")
                .addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
                .addAttribute("xsi:schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9" +
                        "http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");

        //大类别的url匹配规则
        for (SortAndArticleCountVo sort : sortMapper.selectAll(new Page(1, -100)).getRecords()) {
            root.add(createUrlForXML("http://www.uychen.com/sort-list?id=" + sort.getSort().getId()));
        }
        //所有文章的url匹配规则
        for (ArticleAndCommentCountVo article : articleMapper.selectAll(new Page(1, -100)).getRecords()) {
            root.add(createUrlForXML("http://www.uychen.com/article?id=" + article.getArticle().getId()));
        }

        for (Subject subject : subjectMapper.selectAll(new Page(1, -100)).getRecords()) {
            root.add(createUrlForXML("http://www.uychen.com/sj-detail?id=" + subject.getId()));
        }

        return root.asXML();
    }


    /**
     * 创建标签
     *
     * @param url
     * @return
     */
    private String createUrlForHtml(String url, String name) {
        if (!url.startsWith("http://")) {
            url = "http://" + url;
        }
        return "<a href=\"" + url + "\">" + name + "</a><br>";
    }

    public String getResultForHtml() {
        StringBuilder sb = new StringBuilder();
        //大类别的url匹配规则
        for (SortAndArticleCountVo sort : sortMapper.selectAll(new Page(1, -100)).getRecords()) {
            sb.append(createUrlForHtml("http://www.uychen.com/sort-list?id=" + sort.getSort().getId(), sort.getSort().getName()));
        }
        //所有文章的url匹配规则
        for (ArticleAndCommentCountVo article : articleMapper.selectAll(new Page(1, -100)).getRecords()) {
            sb.append(createUrlForHtml("http://www.uychen.com/article?id=" + article.getArticle().getId(), article.getArticle().getTitle()));
        }

        for (Subject subject : subjectMapper.selectAll(new Page(1, -100)).getRecords()) {
            sb.append(createUrlForHtml("http://www.uychen.com/sj-detail?id=" + subject.getId(), subject.getTitle()));
        }
        return sb.toString();
    }

}
